﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Caliburn.Core.Metadata;
using XWord.WPF.ApplicationFramework;
using XWord.Fundamental.Domain;
using System.Threading;
using System.Collections;
using XWord.GUI.Presenters.Interface;
using XWord.Common;
using System.Windows.Documents;
using XWord.GUI.Presenters.Adorns;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Practices.ServiceLocation;
using System.ComponentModel;
using XWord.WPF.ApplicationFramework.Threads;
using XWord.WPF.ApplicationFramework.Extensions;
using System.Windows.Media;
using XWord.GUI.Presenters.Words;
using XWord.GUI.Presenters.Fundamental;
namespace XWord.GUI.Presenters.Behaviors
{
    public class EtymaWordBehavior 
    {
        private ItemsControl _itemsControl;
        public EtymaWordBehavior(ItemsControl adornedElement)    
        {
            _itemsControl = adornedElement;
            _itemsControl.AddHandler(ScrollViewer.ScrollChangedEvent, new ScrollChangedEventHandler(ScrollOwner_ScrollChanged));
        }

        private int verticalOffset;

        void ScrollOwner_ScrollChanged(object sender, ScrollChangedEventArgs e)
        {
            verticalOffset = Convert.ToInt32(e.VerticalOffset);
            if (etymaWordExplain == null || !_itemsControl.HasItems) return;
            Refresh();
        }

        private int GetDisplayCount(ItemsControl itemsControl,int index)
        {
            var item0 = itemsControl.ItemContainerGenerator.ContainerFromIndex(index) as FrameworkElement;
            var count = Math.Ceiling(itemsControl.ActualHeight / item0.ActualHeight);
            return Convert.ToInt32(count);
        }

        public void Refresh()
        {
           
            var index = verticalOffset;

            var count = GetDisplayCount(_itemsControl,verticalOffset);
            var searchText = GetSearchText();
            for (int i = 0; i < count; i++)
            {
                if (index >= _itemsControl.Items.Count) return;
                //if(string.IsNullOrEmpty(searchText);
                var word = _itemsControl.Items[index].ToString().Trim();
                var item = _itemsControl.ItemContainerGenerator.ContainerFromIndex(index++) as ListBoxItem;

                if (etymaWordExplain.ContainsKey(word))
                {
                    var etymaWord = etymaWordExplain[word] as Word;
                    var textblock = item.FindVisualChild<TextBlock>();

                    //distinguish of suffix word
                    MarkSuffixWord(etymaWord, textblock);

                    //highlight search word
                    MarkSearchWord(searchText, word, textblock);
                }
            }
        }

        private void MarkSearchWord(string searchText, string word, TextBlock textblock)
        {
            if (WordTabPresenter.SearchMode == 1 && !string.IsNullOrEmpty(WordTabPresenter.SearchText))
            {
                if (word.Length >= searchText.Length && word.StartsWith(searchText))
                {
                    textblock.Text = string.Empty;
                    textblock.Inlines.Add(new Bold(new Run(searchText)));
                    textblock.Inlines.Add(new Run(word.Substring(searchText.Length)));
                }
            }
            else
            {
                textblock.Text = word;
            }
        }

        private static void MarkSuffixWord(Word etymaWord, TextBlock textblock)
        {
            if (string.IsNullOrEmpty(etymaWord.Suffix))
            {
                textblock.Foreground = Brushes.Gray;
            }
            else
            {
                textblock.Foreground = Brushes.Black;
            }
        }

        private string GetSearchText()
        {
            var searchText = WordTabPresenter.SearchText;
            if (!string.IsNullOrEmpty(searchText))
                searchText = searchText.Split(new char[] { '*' })[0];
            return searchText;
        }

        private Dictionary<string,Word> etymaWordExplain
        {
            get
            {
                return ServiceLocator.Current.GetInstance<IWordExplorerPresenter>().EntityList;
            }
        }

        private WordTabPresenter WordTabPresenter
        {
            get
            {
                return ServiceLocator.Current.GetInstance<WordTabPresenter>();
            }
        } 
    }
}
